home *** CD-ROM | disk | FTP | other *** search
- Path: dns.crocker.com!usenet
- From: johns@crocker.com
- Newsgroups: comp.lang.c++
- Subject: PLEASE help newbie with text file question
- Date: 28 Feb 1996 18:26:32 GMT
- Organization: Crocker Communciations (crocker.com)
- Message-ID: <4h26oo$sot@dns.crocker.com>
- Reply-To: johns@crocker.com
- NNTP-Posting-Host: iplink015.crocker.com
- X-Newsreader: IBM NewsReader/2 v1.2.5
-
- I'm currently teaching myself c++, and am practicing using text files with the
- following small program. This program contains a simple class which stores
- information about a savings project. My problem is that the value I read in as
- the position of 'balance' in the text file does not seem to be right. It seems
- that I get different results if I use seekp or seekg with the same number. What
- I see is that balance is properly written over 0(the first deposit), but that
- additional deposits are places on the same line as the financial goal. (this is
- followed by two eol chars, one from bal line, another from deposit).
- I'm a sophmore in high school and would appreciate ANY suggestions. (the few
- people I know that use c++ stay away from streams). This is NOT homework.
- I program for fun.
- I appologize for this lengthy post, but I honestly don't know how else to figure
- out this problem.
- THANK YOU,
- John Szumowski
- johns@crocker.com
- ************************************************************************
- #include <iostream.h>
- #include <fstream.h>
- #include <stdlib.h>
- #include <values.h>
- #include <ctype.h>
-
- #define MAXREC 10
- #define DATAF "record.txt"
-
- class project {
- long bloc;
- char id[16];
- double balance,goal;
- public:
- void getpos(long &num) {num = bloc; }
- void getbal(double &b) {b = balance;}
- double percomp(double b,double g) {return ((b/g)*100);}
- void deposit(double d) {balance += d; }
- friend istream &operator>>(istream &stream, project &ob);
- friend ostream &operator<<(ostream &stream, project ob);
- friend fstream &operator>>(fstream &stream, project &ob);
- friend fstream &operator<<(fstream &stream, project &ob);
- };
-
- istream &operator>>(istream &stream, project &ob)
- {
- cout << "Enter a id for this project. (15 char max)\n";
- stream.ignore(MAXINT,'\n');
- stream.getline(ob.id,16);
- cout << "Now, enter the financial goal: $";
- stream >> ob.goal;
- ob.balance = 0.0;
- return stream;
- }
-
- ostream &operator<<(ostream &stream, project ob)
- {
- cout << "ID: " << ob.id << endl;
- cout << "Goal: $" << ob.goal << endl;
- cout << "Balance: $" << ob.balance << endl;
- cout << "Percent Complete: " <<
- ob.percomp(ob.balance, ob.goal) << "%\n";
- return stream;
- }
-
- fstream &operator>>(fstream &stream, project &ob)
- {
- if(!stream.eof()) {
- stream.getline(ob.id, 16);
- stream >> ob.goal;
- ob.bloc = stream.tellg();
- stream >> ob.balance;
- stream.ignore(MAXINT,'\n');
- }
- return stream;
- }
-
- fstream &operator<<(fstream &stream, project &ob)
- {
- stream << ob.id << endl;
- stream << ob.goal << endl;
- ob.bloc = stream.tellp();
- stream << ob.balance << endl;
- return stream;
- }
-
- void menu(int &i)
- {
- cout << "*** PROJECT OPTIONS ***\n";
- cout << "1. Create a new savings project\n";
- cout << "2. Deposit into an existing savings project\n";
- //MORE CHOICES...
- cout << "5. Quit\n";
- cout << "\nYour choice: ";
- cin >> i;
- }
-
- main()
- {
- project fp[MAXREC];
- fstream fpdata;
- int numrec = 0,first_rec = 0,i;
- long location[MAXREC];
-
- //initialize array
- fpdata.open(DATAF,ios::in);
- if(!fpdata) {
- cout << "Unable to open data file!";
- exit(1);
- }
- if(fpdata.peek() == EOF) first_rec = 1;
- while(fpdata >> fp[numrec] && numrec < MAXREC) {
- fp[numrec].getpos(location[numrec]);
- numrec++;
- }
- fpdata.close();
- //main program
- do {
- menu(i);
- //new record
- if(i==1) {
- if(first_rec) fpdata.open(DATAF,ios::out);
- else fpdata.open(DATAF,ios::app);
- cin >> fp[numrec];
- fpdata << fp[numrec];
- fp[numrec].getpos(location[numrec]);
- numrec++;
- fpdata.close();
- }
- //deposit into an existing record
- else if(i==2) {
- char ans;
- int where = 0;
- double dep,bal;
- //show records and ask for the one to be deposited into
- do {
- cout << "record number: " << (where+1) << endl;
- cout << fp[where] << endl;
- cout << "Deposit into this record? y/n --->";
- cin >> ans;
- ans = toupper(ans);
- if(ans!='Y') where++;
- }while(where<numrec && ans!='Y');
- //if a record was chosen, ask for deposit and update file
- if(where!=numrec) {
- cout << "Amount to deposit: $";
- cin >> dep;
- fp[where].deposit(dep);
- fp[where].getbal(bal);
- //update bal line in text file
- //opening ios::out destroys previous records
- fpdata.open(DATAF,ios::in | ios::out);
- fpdata.seekp(location[where],ios::beg);
- fpdata << bal << endl;
- fpdata.close();
- }
- }
- } while(i!=5);
- return 0;
- }
- ************************************************************************
-